package mtn.bowen.yan.server.controller;

import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXComboBox;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import mtn.bowen.yan.server.common.Constant;
import mtn.bowen.yan.server.enums.DishesKindEnum;
import mtn.bowen.yan.server.enums.DishesTypeEnum;
import mtn.bowen.yan.server.enums.WeekEnum;
import mtn.bowen.yan.server.fx.StageManager;
import mtn.bowen.yan.server.model.VarietyDishes;
import mtn.bowen.yan.server.service.JavaFxUiService;
import mtn.bowen.yan.server.service.VarietyDishesService;
import mtn.bowen.yan.server.service.WeekRecipeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

/**
 * @author <a href=mailto:yanbowen90@foxmail.com> bowen.yan </a>
 * @version 1.0
 * @description: 修改种类对话框
 * @date 2021-10-21 11:54 PM
 */
@Component
public class EditorKindController extends BaseController implements Initializable {

    private final static Logger LOGGER = LoggerFactory.getLogger(EditorKindController.class);

    @Resource
    private VarietyDishesService dishesService;

    @Resource
    private JavaFxUiService javaFxUiService;

    @FXML
    private VBox vBox;

    @FXML
    private StackPane editorKind;

    private Text text;

    @Resource
    private MainController mainController;

    @Resource
    private WeekRecipeService weekRecipeService;

    @SuppressWarnings("unchecked")
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        try {
            build();
        } catch (Exception e) {
            LOGGER.error("EditorKindController.initialize is error", e);
        }
    }

    public void build() {
        vBox.getChildren().clear();
        vBox.setAlignment(Pos.CENTER);
        VBox.setVgrow(vBox, Priority.ALWAYS);
        vBox.setSpacing(6.0);

        text = javaFxUiService.getText("编辑" + " 【" + StageManager.getTypeAndWeekMap("Week") + "】 【" + StageManager.getTypeAndWeekMap("Type") + "】 " + "食谱数据", Font.font("KaiTi", FontWeight.NORMAL, 26));

        JFXButton submit = javaFxUiService.getButton("提交", "-fx-background-color: -primary;");
        JFXButton cancel = javaFxUiService.getButton("取消", "-fx-background-color: -danger;");

        submit.setOnAction(event -> {
            //选择星期
            WeekEnum week = WeekEnum.getWeek(StageManager.getTypeAndWeekMap("Week"));
            //选择类型
            DishesTypeEnum type = DishesTypeEnum.getDishesType(StageManager.getTypeAndWeekMap("Type"));
            Map<DishesKindEnum, Integer> dishesTypeKindMap = StageManager.getDishesTypeKindMap(type);

            List<VarietyDishes> varietyDishesList = Lists.newArrayList();
            dishesTypeKindMap.forEach((key, value) -> {
                VarietyDishes build = VarietyDishes.builder().type(type.getCode()).kind(key.getCode()).quantity(value).week(week.getCode()).build();
                varietyDishesList.add(build);
            });
            LOGGER.info("EditorKindController.build varietyDishesList: {}", Arrays.toString(new List[]{varietyDishesList}));


            varietyDishesList.forEach(item -> {
                VarietyDishes build = VarietyDishes.builder().type(item.getType()).kind(item.getKind()).quantity(item.getQuantity()).week(item.getWeek()).build();
                dishesService.saveVarietyDishes(build);
                weekRecipeService.deleteByVarietyDishes(item);
            });

            mainController.mainRecipeManagement();
            //隐藏父界面
            StackPane root = StageManager.getParentStackPaneMap("parentStackPane");
            root.setVisible(true);
            javaFxUiService.stageClose("saveEditorDiary");
        });
        cancel.setOnAction(event -> {
            //隐藏父界面
            StackPane root = StageManager.getParentStackPaneMap("parentStackPane");
            root.setVisible(true);
            javaFxUiService.stageClose("saveEditorDiary");
        });

        HBox hBoxText = javaFxUiService.getHBox(450.0, 80.0);
        hBoxText.setAlignment(Pos.CENTER);
        hBoxText.getChildren().add(text);
        vBox.getChildren().add(hBoxText);


        List<VarietyDishes> dishes = dishesService.getDishesByTypeAndWeek(DishesTypeEnum.getDishesType(StageManager.getTypeAndWeekMap("Type")), WeekEnum.getWeek(StageManager.getTypeAndWeekMap("Week")));
        for (int i = 1; i <= Constant.DISHES_KIND; i++) {
            for (VarietyDishes item : dishes) {
                if (item.getKind().equals(i)) {
                    HBox sFoodHBox = javaFxUiService.getHBox(450.0, 8.0);
                    sFoodHBox.setAlignment(Pos.CENTER);
                    Text sFoodText = javaFxUiService.getText(DishesKindEnum.getDishesKind(i).getMsg() + ":");
                    JFXComboBox sFoodComboBox = javaFxUiService.getComboBox(item.getQuantity());
                    sFoodHBox.getChildren().add(sFoodText);
                    sFoodHBox.getChildren().add(sFoodComboBox);
                    //文字与数量之间的间距
                    sFoodHBox.setSpacing(35);
                    vBox.getChildren().add(sFoodHBox);

                    //存储食谱种类信息
                    int selectedIndex = sFoodComboBox.getSelectionModel().getSelectedIndex();
                    DishesKindEnum dishesKind = DishesKindEnum.getDishesKind(i);
                    StageManager.putKindMap(dishesKind, selectedIndex);
                    sFoodComboBox.setId(dishesKind.getMsg());

                    sFoodComboBox.setOnAction(new EventHandler<ActionEvent>() {
                        @Override
                        public void handle(ActionEvent event) {

                            Integer value = (Integer) sFoodComboBox.getValue();
                            System.out.println("sFoodComboBox.getValue(): " + value);
                            String id = sFoodComboBox.getId();
                            System.out.println("sFoodComboBox.id(): " + id);

                            DishesKindEnum kindEnum = DishesKindEnum.getDishesKind(id);
                            StageManager.putKindMap(kindEnum, value);
                            StageManager.putDishesTypeKindMap(DishesTypeEnum.getDishesType(StageManager.getTypeAndWeekMap("Type")), StageManager.getKindMap());

                            System.out.println(new Gson().toJson(StageManager.getDishesTypeKindMap(DishesTypeEnum.getDishesType(StageManager.getTypeAndWeekMap("Type")))));

//                            int type = DishesTypeEnum.getDishesType(StageManager.getTypeAndWeekMap("Type")).getCode();
//                            int week = WeekEnum.getWeek(StageManager.getTypeAndWeekMap("Week")).getCode();
//                            System.out.println("Week: " + WeekEnum.getWeek(StageManager.getTypeAndWeekMap("Week")).getMsg() + "  ,type: " + DishesTypeEnum.getDishesType(StageManager.getTypeAndWeekMap("Type")).getMsg());
//                            VarietyDishes build = VarietyDishes.builder().type(type).kind(kindEnum.getCode()).quantity(value).week(week).build();
//                            dishesService.saveVarietyDishes(build);
                        }
                    });
                }
            }
        }
        StageManager.putDishesTypeKindMap(DishesTypeEnum.getDishesType(StageManager.getTypeAndWeekMap("Type")), StageManager.getKindMap());
        System.out.println(new Gson().toJson(StageManager.getDishesTypeKindMap(DishesTypeEnum.getDishesType(StageManager.getTypeAndWeekMap("Type")))));

        HBox hBoxButton = javaFxUiService.getHBox(450.0, 80.0);
        hBoxButton.setAlignment(Pos.CENTER);
        hBoxButton.getChildren().add(submit);
        hBoxButton.getChildren().add(cancel);
        hBoxButton.setSpacing(145);

        vBox.getChildren().add(hBoxButton);
    }

}
